package main

import "fmt"

func main() {
	getPointerAddress()
	pointerDemo()
	emptyPointer()
	pointerArray()
	pointerOfPointer()
	pointerInFunction()
}
func getPointerAddress() {
	var a int = 10
	fmt.Println(&a)
}
func pointerDemo() {
	var a int = 20
	var ip *int
	ip = &a
	fmt.Println(&a)
	fmt.Println(ip)
	fmt.Println(*ip)
}
func emptyPointer() {
	var a *int
	//<nil>
	fmt.Println(a)
	//下面输出值会报错
	//fmt.Println(*a)
}
func pointerArray() {
	a := []int{10, 100, 1000}
	var i int
	var b [3]*int
	for i = 0; i < 3; i++ {
		b[i] = &a[i]
	}
	for i = 0; i < 3; i++ {
		fmt.Println(*b[i])
	}

}

/**
指针的指针
*/
func pointerOfPointer() {
	var a int
	var b *int
	var c **int
	a = 3000
	b = &a
	c = &b
	fmt.Println(a)
	fmt.Println(*b)
	fmt.Println(b)
	fmt.Println(**c)
	fmt.Println(c)
}
func pointerInFunction() {
	a := 1
	b := 2
	swap(&a, &b)
	fmt.Println(a, b)
}

func swap(x *int, y *int) {
	var temp int
	temp = *x
	*x = *y
	*y = temp
}
